package com.ruoyi.td.service.impl;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.td.config.Constants;
import com.ruoyi.td.dao.TdTemplate;
import com.ruoyi.td.entity.DeviceProperty;
import com.ruoyi.td.entity.DevicePropertyCache;
import com.ruoyi.td.entity.DevicePropertyLogQueryBo;
import com.ruoyi.td.entity.TbDeviceProperty;
import com.ruoyi.td.service.IDeviceInfoData;
import com.ruoyi.td.service.IDevicePropertyData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DevicePropertyDataImpl implements IDevicePropertyData {

    //@Autowired
    //private TdTemplate tdTemplate;


    @Resource
    private IDeviceInfoData deviceInfoData;


    @Override
    public void addProperties(String deviceId, String productKey,Map<String, DevicePropertyCache> properties, long time) {
        StringBuilder sbFieldNames = new StringBuilder();
        StringBuilder sbFieldPlaces = new StringBuilder();
        List<Object> args = new ArrayList<>();
        args.add(time);

        //组织sql
        properties.forEach((key, val) -> {
            sbFieldNames.append(key)
                    .append(",");
            sbFieldPlaces.append("?,");
            args.add(val.getValue());
        });
        sbFieldNames.deleteCharAt(sbFieldNames.length() - 1);
        sbFieldPlaces.deleteCharAt(sbFieldPlaces.length() - 1);

        String sql = String.format("INSERT INTO %s (time,%s) USING %s TAGS ('%s') VALUES (?,%s);",
                Constants.getDevicePropertyTableName(deviceId),
                sbFieldNames.toString(),
                Constants.getProductPropertySTableName(productKey),
                deviceId,
                sbFieldPlaces.toString());

        //tdTemplate.update(sql, args.toArray());

    }

    @Override
    public List<DeviceProperty> findDevicePropertyHistory(DevicePropertyLogQueryBo logQueryBo) {
        String tbName = Constants.getDevicePropertyTableName(logQueryBo.getDeviceId());
        List<TbDeviceProperty> deviceProperties =new ArrayList<>();
        try {
            //deviceProperties= tdTemplate.query(String.format(
            //                "select time,%s as `value`,device_id from %s where device_id=? and time>=? and time<=? " +
            //                        "order by time asc limit 0," + 10000,
            //                logQueryBo.getName().toLowerCase(), tbName),
            //        new BeanPropertyRowMapper<>(TbDeviceProperty.class),
            //        logQueryBo.getDeviceId(), logQueryBo.getStart(), logQueryBo.getEnd()
            //);

        }catch (Exception e){
            log.info("查询数据库异常");
            e.printStackTrace();
        }
        return deviceProperties.stream().map(p -> new DeviceProperty(
                        p.getTime().toString(),
                        p.getDeviceId(),
                        logQueryBo.getName(),
                        p.getValue(),
                        p.getTime()))
                .collect(Collectors.toList());
    }
}
